package com.witmore.neutron.ai.web.controller;

import com.witmore.neutron.ai.web.controller.bo.MessageBO;
import com.witmore.neutron.ai.web.controller.bo.MessageWebParamNew;
import com.witmore.neutron.ai.web.filter.AgentLoginRequired;
import com.witmore.neutron.ai.web.filter.GlobalAccountSession;
import com.witmore.neutron.ai.web.service.ChatClientRouterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.codec.ServerSentEvent;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

/**
 * Description: ChatStreamController
 * Author: 豆子高
 * Date: 2025/5/15 15:03
 */
@Slf4j
@Controller
@RequestMapping("/chat/stream")
public class ChatStreamController {

    @Autowired
    private ChatClientRouterService chatClientRouterService;

    @AgentLoginRequired(value = "agentCode")
    @ResponseBody
    @PostMapping(value = "/{agentCode}", produces = "text/event-stream;charset=utf-8")
    public Flux<ServerSentEvent<String>> chatStream(@PathVariable("agentCode") String agentCode, @RequestBody MessageWebParamNew param) {
        log.warn("param:{}", param);
        MessageBO messageBO = new MessageBO();
        messageBO.setAgentCode(agentCode);
        messageBO.setAgentId(param.getAgentId());
        messageBO.setMessages(param.getMessages());
        messageBO.setStream(true);
        messageBO.setSource(param.getSource());
        messageBO.setSessionId(param.getSessionId());
        messageBO.setUserId(GlobalAccountSession.getUserInfo().getId());
        return chatClientRouterService.routeRequestSSEStream(messageBO);
    }

}
